/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.UserBean;
import dao.UserDAO;
import java.io.IOException;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 *
 * @author amkreuz
 */
@Named(value = "userController")
@SessionScoped
public class UserController implements Serializable {

    private UserBean theModel;
    private boolean loggedIn = false;

    public UserBean getTheModel() {
        return theModel;
    }

    public void setTheModel(UserBean theModel) {
        this.theModel = theModel;
    }

    /**
     * Creates a new instance of ProfileController
     */
    public UserController() {
        theModel = new UserBean();
    }

    public String login() {

        try {
            UserDAO.init();
            FacesContext.getCurrentInstance().
                    getExternalContext().getSessionMap().put("userBean", theModel);
            String temp = UserDAO.getPassword(theModel);
            if (!(temp.equals(theModel.getPassword()))) {
                return "IncorrectLogin.html";
            }
            UserDAO.updateUserBean(theModel);
            UserDAO.terminate();
            loggedIn = true;

        } catch (SQLException ex) {
            Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
            return "IncorrectLogin.html";
        }
        if (theModel.getHouseName() == null) {
            return "JoinCreateHousehold.xhtml";
        } else {
            return "UserHomePage.xhtml";
        }

    }

    public void nulify() {
        theModel.setHouseName(null);
        theModel.setIsAdmin(false);
        theModel.setPassword(null);
        theModel.setUserName(null);
    }

    public String createAccount() {
        try {
            UserDAO.init();

            UserDAO.createUser(theModel);

            FacesContext.getCurrentInstance().
                    getExternalContext().getSessionMap().put("userBean", theModel);
        } catch (SQLException ex) {
            Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                UserDAO.terminate();
            } catch (SQLException ex) {
                Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
            }
            loggedIn = true;
            return "JoinCreateHousehold.xhtml";
        }
    }

    public void changePassword() {
        try {
            UserDAO.init();

            UserDAO.setPassword(theModel);
            FacesContext.getCurrentInstance().addMessage("changePassword", new FacesMessage("Password Changed"));
        } catch (SQLException ex) {
            Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                UserDAO.terminate();
            } catch (SQLException ex) {
                Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }

    public String deleteAccount() {
        try {
            UserDAO.init();
            UserDAO.deleteAccount(theModel);
            theModel.setHouseName(null);
            theModel.setIsAdmin(false);
            theModel.setPassword(null);
            theModel.setPoints(0);
            theModel.setUserName(null);
            UserDAO.terminate();
            return "index.xhtml";
        } catch (SQLException ex) {
            Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
            try {
                UserDAO.terminate();
            } catch (SQLException ex1) {
                Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex1);
            }
            loggedIn = false;
            return "AccountManager.xhtml";
        }
    }

    public String joinHousehold() {
        try {
            UserDAO.init();

            UserDAO.joinHousehold(theModel);
            UserDAO.terminate();
            return "UserHomePage.xhtml";

        } catch (SQLException ex) {
            Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
            return "JoinCreateHousehold.xhtml";
        }
    }

    public String leaveHousehold() {
        try {
            UserDAO.init();

            UserDAO.leaveHousehold(theModel);
            UserDAO.terminate();
            return "JoinCreateHousehold.xhtml";
        } catch (SQLException ex) {
            Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
            return "";
        }
    }

    public void leaveHousehold(UserBean bean) {
        try {
            UserDAO.init();

            UserDAO.leaveHousehold(bean);
            UserDAO.terminate();

        } catch (SQLException ex) {
            Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);

        }
    }
}
